import request from '@/plugins/request'
import md5 from 'md5'

const CacheObj = require('../plugins/global-cache')

//request的功能实现，启用了缓存功能
export function requestAutoCache (requestParams) {
  // console.log('requestAutoCache-requestParams=', requestParams)
  let _k = md5('api:' + JSON.stringify(requestParams))
  let _v = CacheObj.get(_k)
  if (_v) {
    console.log('api-cache-exists =', _k)
    // return _v
    return Promise.resolve(_v)
  }

  return request(requestParams).then(res => {
    // console.log('requestAutoCache-Result=', res)
    if (res && res.code == 1) {
      CacheObj.set(_k, res)
      // console.log('api-cache-set =', _k)
    }
    return Promise.resolve(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}
